package com.cloudlead.common.lang.persistence;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EasyuiGridData implements java.io.Serializable {

    private Long total;// 总记录数
    private List rows;// 每行记录
    private List footer;
    private String errorMsg="";

    public EasyuiGridData(){
        this.total=(long)0;
        this.rows= new ArrayList();
    }

    public EasyuiGridData(Page page){
        this.total= page.getTotalElements();
        this.rows = page.getContent();
    }


    public EasyuiGridData(Page page,String columns){
        this.total= page.getTotalElements();
        List list= new ArrayList();
        for (Object o:page.getContent()){
            HashMap map = new HashMap();
            for (String fld:columns.split(",")) {
                fld = fld.trim();
                while (fld.indexOf("  ")>=0) fld=fld.replaceAll("  "," ");
                String fName=fld;
                if (fld.indexOf(" ")>0){
                    fName = fld.substring(fld.indexOf(" ")+1);
                    fld = fld.substring(0,fld.indexOf(" "));
                }
                if (fld.indexOf(".")>0){
                    String[] exFld= fld.split("\\.");
                    Object childObj=o;
                    for (int i=0;i<exFld.length-1;i++) {
                        childObj = getFieldValue(childObj, exFld[i]);
                    }
                    map.put(fName,getFieldValue(childObj,exFld[exFld.length-1]));
                }else{
                    map.put(fName,getFieldValue(o,fld));
                }
            }
            list.add(map);
        }
        this.rows= list;
    }

    public EasyuiGridData(String errMsg){
        this.total=(long)0;
        this.rows= new ArrayList();
        this.errorMsg=errMsg;
    }

    public EasyuiGridData (long gridTotal,List gridRows,List gridFoots){
        this.setTotal(gridTotal);
        this.setRows(gridRows);
        if (gridFoots!=null) this.setFooter(gridFoots);
        if (this.getRows()!=null)
        for(Object o: this.getRows()){
            HashMap<String,Object> map = (HashMap<String,Object>)o;
            for (String key:map.keySet())
                if (map.get(key)==null) map.put(key,"");
        }
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    public List getFooter() {
        return footer;
    }

    public void setFooter(List footer) {
        this.footer = footer;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    private Object getFieldValue(Object obj,String fldName){
        if (obj==null) return null;
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for(Field f : fields) {
                f.setAccessible(true);
                if (f.getName().equalsIgnoreCase(fldName))
                    return f.get(obj);
            }
        } catch (IllegalAccessException e) {
            return null;
        }
        return null;
    }

}
